//
//  untitled.m
//  MClient
//
//  Created by apple_cyg on 10-11-21.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ClassAddition.h"
#import "MCUIToolkit.h"
#import "MCImageView.h"
#import "FieldAction.h"
//#import "LocationView.h"
#import "TextFieldViewController.h"
#import "FileViewController.h"
#import "FileListViewController.h"
#import "DateTimeSelectView.h"
#import "LookupSelectView.h"
@implementation ViewFormController(ViewFormControllerAddition)

/*
- (NSInteger) cellHeight:(NSMutableDictionary*)appItem
		  withLableStyle:(int)lableStyle 
			   withTitle:(NSString*)aTitle 
			   withValue:(NSString*)aValue
			   fieldType:(NSString*)fieldType
{
	float fontSize = 16.0;
	NSMutableDictionary *style = [appItem objectForKey:@"style"];
	if (style != nil) {
		if ([style objectForKey:@"font_size"]) {
			fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
		}
	}
	if (lableStyle == 1) {//标题、内容换行
		CGSize size = [aValue sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] constrainedToSize:CGSizeMake(300, 9999)];
		int nLine = size.height/(fontSize+4);
		
		if (nLine == 0) {
			return 44;
		} else {
			if ([@"8" isEqualToString:fieldType] || [@"G" isEqualToString:fieldType]) {
				if (nLine > 3) {
					nLine = 3;
					[appItem setObject:[NSNumber numberWithBool:YES] forKey:@"needDisclosureIndicator"];
				}
			}
			return nLine*(fontSize+4)+22 + 25;
		}
	} else if (lableStyle == 2) {//标题隐藏，内容换行
		CGSize size = [aValue sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] constrainedToSize:CGSizeMake(300, 9999)];
		int nLine = size.height/(fontSize+4);
		if ([@"8" isEqualToString:fieldType] || [@"G" isEqualToString:fieldType]) {
			if (nLine > 3) {
				nLine = 3;	
				[appItem setObject:[NSNumber numberWithBool:YES] forKey:@"needDisclosureIndicator"];
			}
		}
		return nLine*(fontSize+4)+ 22;
	} else {
		return 44;
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"] 
									  objectAtIndex:indexPath.section]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	NSString* aTitle = [MCUIToolkit getFieldLabel:appItem];
	NSString* aValue = [MCUIToolkit getFieldTextValue:formDataCopy
											  appItem:appItem
										 selectOption:[formDataCopy objectForKey:@"so"]];
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	if (cFieldType == 'S') {
		NSString *name = [MCUIToolkit getFieldName:appItem];
		NSMutableDictionary *fieldData = [[formDataCopy objectForKey:@"data"] objectForKey:name];
		NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
		if (fileInfos == nil || [fileInfos count] < 1) 
			return [self cellHeight:appItem withLableStyle:lableStyle withTitle:aTitle withValue:aValue fieldType:fieldType];
		NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
		NSString *fileName = [[NSString stringWithFormat:@"indexPath_Seciton%d_Row%d_%@", indexPath.section,
							   indexPath.row, [fileInfo objectForKey:@"fname"]] retain];
		MCSize *size = [imageSizeMap objectForKey:fileName];
		if (size && size.height > 0) {
			if (lableStyle > 0) {
				if (size.width < 300.0) {
					return size.height+20;
				}else {
					return size.height/(size.width/300.0)+20;
				}
			}else {
				if (size.width < 230.0) {
					return size.height+20;
				}else {
					return size.height/(size.width/230.0)+20;
				}
			}
		}
	} else if (cFieldType == '0') {
		return CGRectGetHeight(clientView.frame) / 3 + 10;
	}
	return [self cellHeight:appItem withLableStyle:lableStyle withTitle:aTitle withValue:aValue fieldType:fieldType];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	// 主表单
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"] 
									  objectAtIndex:indexPath.section]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
	
	UITableViewCell *cell = [MCFunction createCellForViewForm:tableView 
													  appItem:appItem
													indexPath:indexPath 
													 formView:self.clientView];
	if ([[appItem objectForKey:@"needDisclosureIndicator"] boolValue]) {
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
	}
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	UILabel *labelField = (UILabel *)[cell.contentView viewWithTag:-1];
	labelField.text = [MCUIToolkit getFieldLabel:appItem];
	
	if (cFieldType != 'S') {
		NSString *valueText = [MCUIToolkit getFieldTextValue:formDataCopy
													 appItem:appItem
												selectOption:[formDataCopy objectForKey:@"so"]];
		
		if (cFieldType == '0') {
			@try {
				UIWebView *webView = (UIWebView *)[cell.contentView viewWithTag:-2];
				if (nil != valueText && ![@"" isEqualToString: valueText]) {
					[webView loadHTMLString:valueText baseURL:nil];
				}
				return cell;
			}
			@catch (NSException * e) {
			}
		}
		UILabel *valueField = (UILabel *)[cell.contentView viewWithTag:-2];
		NSMutableDictionary *style = [appItem objectForKey:@"style"];
		if (style != nil) {
			float fontSize = 16.0;
			if ([style objectForKey:@"font_color"]) {
				valueField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"font_color"]]];
			}
			if ([style objectForKey:@"font_size"]) {
				fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
				valueField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
			if ([style objectForKey:@"font_style"]) {
				NSString *fontStyle = [style objectForKey:@"font_style"];
				if ([fontStyle isEqualToString:@"B"]) {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}else if ([fontStyle isEqualToString:@"I"]) {
					valueField.font = [UIFont italicSystemFontOfSize:fontSize];
				}else {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}
			}
		}
		
		if (cFieldType == 'H'||cFieldType == 'I'||cFieldType == 'K'||cFieldType == 'L') {
			NSArray *selectIcons = [valueText componentsSeparatedByString:@"::"];
			if ([selectIcons count] > 1) {
				NSString *iconLabel = [selectIcons objectAtIndex:1];
				NSArray *icons = [iconLabel componentsSeparatedByString:@","];
				for (int j = 0; j < [icons count]; j++) {
					NSString *icon = [icons objectAtIndex:j];
					UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(32 * j, 0, 24, 24)];
					imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
					imageView.backgroundColor = [UIColor clearColor];
					[valueField addSubview:imageView];
					[imageView release];
				}
				return cell;
			} 
		}
		valueField.text = valueText;
	} else {
		NSString *name = [MCUIToolkit getFieldName:appItem];
		NSMutableDictionary *fieldData = [[formDataCopy objectForKey:@"data"] objectForKey:name];
		NSString *dataFrom = [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
		NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
		if (fileInfos == nil || [fileInfos count] < 1) 
			return cell;
		NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
		NSString *fileName = [fileInfo objectForKey:@"fname"];
		if (fileName == nil || [fileName length] == 0)
			return cell;
		NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
		
		NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											  fileName,@"fileName",
											  fileLocation,@"fileLocation",
											  @"download",@"actionType",
											  dataFrom,@"dataFrom",
											  [NSString stringWithFormat:@"%d", indexPath.section], @"indexPath_section",
											  [NSString stringWithFormat:@"%d", indexPath.row], @"indexPath_row",
											  nil];
		MCImageView *valueField = (MCImageView *)[cell.contentView viewWithTag:-2];
		if (valueField.image == nil) {
			valueField.fileAttribute = fileAttribute;
			[fileAttribute release];
		}else {
			int lableStyle = [[appItem objectForKey:@"lbls"] intValue];
			if (lableStyle > 0) {
				if (valueField.image.size.width < 300) {
					valueField.frame = CGRectMake(10, lableStyle==1?40:10, valueField.image.size.width, 35);
				}
			}else {
				if (valueField.image.size.width < 230) {
					valueField.frame = CGRectMake(80, lableStyle==1?40:10, valueField.image.size.width, 35);
				}
			}
		}
	}
	return cell;
}	*/
@end

@implementation MCFunction(MCFunctionAddition)
+ (void)selectDetailForViewForm:(UIViewController*)viewController
					   withCell:(UITableViewCell *)aCell 
				  withIndexPath:(NSIndexPath*)aIndexPath
						appItem:(NSMutableDictionary *)appItem 
						   data:(NSMutableDictionary *)fieldData {
	
	NSMutableDictionary *field = [MCUIToolkit getField:appItem];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	
	switch (cFieldType) {
		case '1': //公式
		case '2': //自动编号
		case '3': //主－详细关系
		case '4': //查找关系
		case '6': //百分比
		case '7': //金额
		case 'E': //数字
		case 'B': //复选框
		case 'C': //日期
		case 'D': //日期时间
			break;
			
		case '5': //超级链接
		{
			[MCFunction goUrl:viewController withUrl:[fieldData objectForKey:@"value"]];
		}
			break;
		case '9': //电话
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
				fieldAction.actionUrls = [[NSMutableArray alloc] 
										  initWithObjects:
										  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
										  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
				[actionSheet addButtonWithTitle:@"拨打电话"];
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话!"];
			}
		}
			break;
		case 'A': //电子邮件
		{
			FieldAction *fieldAction = [[FieldAction alloc] init];
			fieldAction.viewController = viewController;
			fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
									  [NSString stringWithFormat:@"mailto:%@",[fieldData objectForKey:@"value"]],
									  nil];
			UIActionSheet *actionSheet = [[UIActionSheet alloc]
										  initWithTitle:@"请选择操作"
										  delegate:fieldAction
										  cancelButtonTitle:nil
										  destructiveButtonTitle:nil 
										  otherButtonTitles:nil];
			[actionSheet addButtonWithTitle:@"发送邮件"];
			int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
			actionSheet.cancelButtonIndex = cancelIndex;
			[actionSheet showInView:viewController.clientView.window];
			[actionSheet release];
			
		}
			break;
		case 'Q': //手机号码
		{
			NSString *machineMode = [MCFunction machine];
			if ([machineMode hasPrefix:@"iPhone"]) {
				FieldAction *fieldAction = [[FieldAction alloc] init];
				fieldAction.viewController = viewController;
				fieldAction.actionUrls = [[NSMutableArray alloc] initWithObjects:
										  [NSString stringWithFormat:@"tel://%@",[fieldData objectForKey:@"value"]],
										  [NSString stringWithFormat:@"sms://%@",[fieldData objectForKey:@"value"]],
										  nil];
				UIActionSheet *actionSheet = [[UIActionSheet alloc]
											  initWithTitle:@"请选择操作"
											  delegate:fieldAction
											  cancelButtonTitle:nil
											  destructiveButtonTitle:nil 
											  otherButtonTitles:nil];
				[actionSheet addButtonWithTitle:@"拨打电话"];
				[actionSheet addButtonWithTitle:@"发送短信"];
				int cancelIndex = [actionSheet addButtonWithTitle:@"取消"];
				actionSheet.cancelButtonIndex = cancelIndex;
				[actionSheet showInView:viewController.clientView.window];
				[actionSheet release];
			}
			else {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"只能在手机上才能打电话和发短信!"];
			}
		}
			break;
			
		case 'F': //文本
		case '8': //文本区
		case 'G': //文本区
		{
			TextFieldViewController *textFieldView = [[TextFieldViewController alloc] init];
			textFieldView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			
			textFieldView.isReadOnly = YES;
			textFieldView.fieldData = fieldData;
			textFieldView.field = field;
			[viewController.navigationController pushViewController:textFieldView animated:YES];
			[textFieldView release];
		}
			break;
			
		case 'H': //单选列表（下拉框）
		case 'I': //多选列表（列表框）
		case 'K': //单选列表（单选按钮）
		case 'L': //多选列表（复选框）
		case 'O': //密码
			break;
			
		case 'S': //图片
		case 'J': //文件
		{
			NSString *dataFrom = [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
			NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
			if ([fileInfos count] != 1) {
				return;
			}
			NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
			NSString *fileName = [fileInfo objectForKey:@"fname"];
			if (fileName == nil || [fileName length] == 0)
				return;
			if ([fileName hasSuffix:@"avi"]) {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"视频文件暂不支持播放功能!"];
				return;
			}
			NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
			NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												  fileName,@"fileName",
												  fileLocation,@"fileLocation",
												  @"download",@"actionType",
												  dataFrom,@"dataFrom",nil];
			FileViewController *fileView = [[FileViewController alloc] init];
			[viewController.navigationController pushViewController:fileView animated:YES];
			fileView.title = @"文件下载中...";
			fileView.fileAttribute = fileAttribute;
			[fileAttribute release];
			[fileView getDataFromServer];
			[fileView release];
			return;
		}
			break;
		case '0': //html
		{
			NSString* htmlString = [fieldData objectForKey:@"value"];
			MCWebViewController* htmlView = [[MCWebViewController alloc] init];
			[viewController.navigationController pushViewController:htmlView animated:YES];
			[htmlView loadHTMLString:htmlString];
			[htmlView release];
		}
			break;
		case 'T': //手机定位
		{
//			LocationView *locationView = [[LocationView alloc] init];
//			locationView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
//			
//			locationView.fieldData = fieldData;
//			locationView.field = field;
//			locationView.isViewMode = YES;
//			
//			[viewController.navigationController pushViewController:locationView animated:YES];
//			[locationView release];
		}
			break;
			
		case 'Y': //文件（多个）
		{
			if (nil == [fieldData objectForKey:@"fi"] || 0 == [[fieldData objectForKey:@"fi"] count]) {
				return;
			}
			FileListViewController *fileListView = [[FileListViewController alloc] initWithStyle:UITableViewStylePlain];
			fileListView.valueField = (MCControl *)[aCell.contentView viewWithTag:-2];
			
			fileListView.fieldData = fieldData;
			fileListView.field = field;
			fileListView.isViewMode = YES;
			
			[viewController.navigationController pushViewController:fileListView animated:YES];
			[fileListView release];
		}
			break;
			
		case 'M': //精确扫描
		case 'N': //简易扫描
		case 'P': //智能卡
		case 'R': //条形码
		case 'U': //多选过滤
		case 'V': //gps
		case 'W': //彩码
		case 'X': //流媒体
			break;
		default:
			break;
	}
}
@end





#pragma mark----MCLoginView--------------用于兼容ios6的渲染效果--------------------------------
@implementation MCLoginView(MCLoginViewAddition)

- (id)initWithPoint:(CGPoint)point
{
	CGRect frame = CGRectMake(point.x, point.y, kLoginFormBackgroundWidth, kLoginFormBackgroundHeight_Demo);//kLoginFormBackgroundWidth等是login工作内容区的大小定义。
	if (self = [super initWithFrame:frame]) {
		_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
		_tableView.backgroundColor = [UIColor clearColor];
		_tableView.allowsSelection = NO;
		_tableView.scrollEnabled = NO;
		_tableView.dataSource = self;
		_tableView.delegate = self;
		_tableView.separatorColor = self.tableSeparatorColor;
        //		白菜 下面的代码就是 兼容代码
        _tableView.backgroundView = nil;
		if ([MCUserDefaults instance].isHidePartyId) {//隐藏会员名
			_partyIdField = [[UITextField alloc] initWithFrame:CGRectZero];
			_partyIdField.text = [MCUserDefaults instance].current_PartyId;
		}
	}
	
	return self;
}

@end

#pragma mark----MenuController------------用于兼容ios6的渲染效果-------------
@implementation MenuController(MenuControllerAddition)
- (void)viewDidLoad {
	[super viewDidLoad];
	// 添加左右按钮
	self.navigationController.navigationItem.hidesBackButton = NO;
    
	if (!self.userDefaults.guestDefaultLogin && !self.isShortcutMore) {
		if (isApp) {//主菜单页面
			[self leftBarButton:@"重新登录" withStyle:UIBarButtonItemStylePlain selector:@selector(logout:)];//huotianjun注销改为重新登录
			if (self.userDefaults.needModifyPassword) {
				[self rightBarButton:@"修改密码" withStyle:UIBarButtonItemStyleDone selector:@selector(modifyPassword:)];
			}
		}// else {//各子菜单页面
		//	[self rightBarButton:@"注销" withStyle:UIBarButtonItemStyleDone selector:@selector(logout:)];
		//}
	}
	
	self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.backgroundView = nil;
	//CGRect rc = CGRectMake(0, 0, nScreenWidth, CGRectGetMinY(self.appDelegate.shortcutsToolBar.frame));
	//self.tableview.frame = CGRectMake(0, 0, nScreenWidth, CGRectGetMinY(self.appDelegate.shortcutsToolBar.frame));
	// 显示帮助
	self.addHelpButton = YES;
	self.helpUrl = @"/iphone/help/chapter03.html";
	
	// 是九宫格菜单吗
	if ([self isGridMenu]) {
		[self initGridMenus];
		self.tableview.delegate = nil;
		self.tableview.dataSource = nil;
	}
	[self initTimer];
}


@end


@implementation LoginController(LoginControllerAddition)

//       调用自动升级的类
- (void)viewWillAppear:(BOOL)animated {
    
    
    GeAutoUpdate *geauto = [[GeAutoUpdate alloc] initWithClientEdition];
    [geauto getJSonDataFromServer];
    
	[super viewWillAppear:animated];
    
	self.navigationController.navigationBar.tintColor = self.userDefaults.skin_navigationBarColor;
    
    [self reloadFieldValues];//huotianjun
	
	if (loginView) {
		UINavigationBar* naviBar = (UINavigationBar*)[self.view viewWithTag:-200];
		naviBar.tintColor = self.navigationController.navigationBar.tintColor;
		naviBar.hidden = YES;
		
		bViewWillApper = YES;
		[self didRotateFromInterfaceOrientation:0];
		bViewWillApper = NO;
		
		[loginView refreshImages];
		UIImageView *backgroundView = (UIImageView*)[self.view viewWithTag:-100];
		backgroundView.image = self.userDefaults.skin_bgLoginImg;
		UIImageView *logoImageView = (UIImageView*)[self.view viewWithTag:-1];
		logoImageView.image = self.userDefaults.skin_loginlogoImg;
		UIImageView *titleImageView = (UIImageView*)[self.view viewWithTag:-2];
		titleImageView.image = self.userDefaults.skin_logintitleImg;
	} else {
		[self.navigationController setNavigationBarHidden:YES animated:NO];
	}
	[self hideNavigationBar];
	
	//[self.appDelegate stopActivityIndicatorView];
}



@end



@implementation MCUrlProcess(MCUrlProcessAddition)
- (void) getViewFormConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *layoutName = [url objectForKey:@"lytn"];
		
		NSMutableArray *initialValues = nil;
		if ([url objectForKey:@"qflt"]) {
			initialValues = [url objectForKey:@"qflt"];
		} else {
			initialValues = [NSMutableArray array];
		}
        
		NSString *keyValue = [url objectForKey:@"pkey"];
		if (keyValue == nil) {
			keyValue = @"";
		}
		
		if (NO) {
			AViewFormController *viewFormController = [[AViewFormController alloc]initWithStyle:UITableViewStylePlain];
			viewFormController.dataObjectName = dataObjectName;
			viewFormController.layoutName = layoutName;
			viewFormController.keyValue = keyValue;
			viewFormController.initialValues = initialValues;
			viewFormController.formLayout = returnValue;
			[jump2NaviController pushViewController:viewFormController animated:YES];
			[viewFormController getDataFromServer];
			
			[viewFormController release];
			
		}
		else {
			ViewFormController *viewFormController = [[ViewFormController alloc]initWithStyle:UITableViewStyleGrouped];
            
            UIImageView* background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
            background.image = [MCUserDefaults instance].skin_bgViewImg;
            [viewFormController.tableview setBackgroundView:background];
            
			viewFormController.dataObjectName = dataObjectName;
			viewFormController.layoutName = layoutName;
			viewFormController.keyValue = keyValue;
			viewFormController.initialValues = initialValues;
			viewFormController.formLayout = returnValue;
			[jump2NaviController pushViewController:viewFormController animated:YES];
			[viewFormController getDataFromServer];
			
			[viewFormController release];
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99"
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}



// 可编辑的表单也变成新样式
- (void) getEditFormConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *layoutName = [url objectForKey:@"lytn"];
		NSMutableArray *initialValues = nil;
		if ([url objectForKey:@"qflt"]) {
			initialValues = [url objectForKey:@"qflt"];
		} else {
			initialValues = [NSMutableArray array];
		}
        
		NSString *keyValue = [url objectForKey:@"pkey"];
		if (keyValue == nil) {
			keyValue = @"";
		}
		NSLog(@"url=====~ %@",url);
		EditFormController *editFormController = [[[[MCUserDefaults instance] class:@"EditFormController"] alloc] initWithStyle:UITableViewStyleGrouped];
		
		editFormController.dataObjectName = dataObjectName;
		editFormController.layoutName = layoutName;
		editFormController.keyValue = keyValue;
		editFormController.initialValues = initialValues;
		editFormController.formLayout = returnValue;
        
		[jump2NaviController pushViewController:editFormController animated:YES];
		[editFormController getDataFromServer];
		[editFormController release];
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99"
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}



@end


#pragma mark ********** 经纬度 **********
@implementation EditFormController(EditFormControllerAddition)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	int sectionIndex = indexPath.section;
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		sectionIndex = tabIndex;
	}
	
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"]
									  objectAtIndex:sectionIndex]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
    
   //新加 补救appitem yuxiaotong
    id fld = [appItem objectForKey:@"fld"];
    NSString * name = [fld objectForKey:@"name"];
    
    
    id so = [formData objectForKey:@"so"];
    id itemSo = nil;
    if (nil != so) {
        itemSo = [so objectForKey:name];
    }    
        
    if (itemSo ) {
        [[appItem objectForKey:@"fld"] setObject:itemSo forKey:@"so"];
    }
        
	NSString *valueText = [MCUIToolkit getFieldTextValue:self.formData
												 appItem:appItem
											selectOption:[formLayoutCopy objectForKey:@"so"]];
	
	NSString *tipHolder = [[appItem objectForKey:@"style"] objectForKey:@"tipholder"];

	UITableViewCell *cell = [MCFunction createCellForEditForm:tableView
													  appItem:appItem
													indexPath:indexPath
													 formView:self.clientView];
    
        UILabel *labelField = (UILabel *)[cell.contentView viewWithTag:-1];
        labelField.text = [MCUIToolkit getFieldLabel:appItem];

//    	NSLog(@"---title=%@, value=%@---", labelField.text, valueText);
    //	NSLog(@"-self.formData=%@----,appItem=%@", self.formData, appItem);
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);

	if (cFieldType != 'B') {
		if (cFieldType == '0') {
			@try {
				UIWebView *webView = (UIWebView *)[cell.contentView viewWithTag:-2];
				if (nil != valueText && ![@"" isEqualToString:valueText]) {
					[webView loadHTMLString:valueText baseURL:nil];
				}
				return cell;
			}
			@catch (NSException * e) {
			}
		}
		if (cFieldType == 'S' && [[appItem objectForKey:@"rdy"] boolValue]) {
			NSString *name = [MCUIToolkit getFieldName:appItem];
			NSMutableDictionary *fieldData = [[formData objectForKey:@"data"] objectForKey:name];
//            NSLog(@"name3=%@fieldData3=%@",name,fieldData);
			NSString *df= [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
			NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
			if (fileInfos == nil || [fileInfos count] < 1)
				return cell;
			NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
			NSString *fileName = [fileInfo objectForKey:@"fname"];
			if (fileName == nil || [fileName length] == 0)
				return cell;
			NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
			
			NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												  fileName,@"fileName",
												  fileLocation,@"fileLocation",
												  @"download",@"actionType",
												  df,@"dataFrom",
												  [NSString stringWithFormat:@"%d", indexPath.section], @"indexPath_section",
												  [NSString stringWithFormat:@"%d", indexPath.row], @"indexPath_row",
												  nil];
			MCImageView *valueField = (MCImageView *)[cell.contentView viewWithTag:-2];
			
			NSMutableDictionary *field = [MCUIToolkit getField:appItem];
			if([field objectForKey:@"imgw"] && [field objectForKey:@"imgh"]) {
				valueField.imgw = [[field objectForKey:@"imgw"] intValue];;
				valueField.imgh = [[field objectForKey:@"imgh"] intValue];
			}
			
			if (valueField.image == nil) {
				valueField.fileAttribute = fileAttribute;
				[fileAttribute release];
			}else {
				int lableStyle = [[appItem objectForKey:@"lbls"] intValue];
				if (lableStyle > 0) {
					if (valueField.image.size.width < 300) {
						valueField.frame = CGRectMake(10, lableStyle==1?40:10, valueField.image.size.width, 35);
					}
				}else {
					if (valueField.image.size.width < 200) {
						valueField.frame = CGRectMake(110, lableStyle==1?40:10, valueField.image.size.width, 35);
					}
				}
			}
			return cell;
		}
		MCControl* valueField = (MCControl *)[cell.contentView viewWithTag:-2];
        
		if (tipHolder && valueField.style == MCControl_TextField) {
			((UITextField*)valueField.control).placeholder = tipHolder;
		}
		NSMutableDictionary *style = [appItem objectForKey:@"style"];
		if (style != nil) {
			float fontSize = 16.0;
			if ([style objectForKey:@"font_color"]) {
				valueField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"font_color"]]];
			}
			if ([style objectForKey:@"font_size"]) {
				fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
				valueField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
			if ([style objectForKey:@"font_style"]) {
				NSString *fontStyle = [style objectForKey:@"font_style"];
				if ([fontStyle isEqualToString:@"B"]) {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}else if ([fontStyle isEqualToString:@"I"]) {
					valueField.font = [UIFont italicSystemFontOfSize:fontSize];
				}else {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}
			}
			if ([style objectForKey:@"font_align"]) {
				NSString *fontAlign = [style objectForKey:@"font_align"];
				if ([fontAlign isEqualToString:@"L"]) {
					valueField.textAlignment = UITextAlignmentLeft;
				}else if ([fontAlign isEqualToString:@"R"]) {
					valueField.textAlignment = UITextAlignmentRight;
				}else if ([fontAlign isEqualToString:@"M"]) {
					valueField.textAlignment = UITextAlignmentCenter;
				}
			}
		}
		if (cFieldType == 'H'||cFieldType == 'I'||cFieldType == 'K'||cFieldType == 'L') {
			NSArray *selectIcons = [valueText componentsSeparatedByString:@"::"];
			if ([selectIcons count] > 1) {
				for(UIImageView *imageView in [valueField subviews]){
					[imageView removeFromSuperview];
				}
				NSString *iconLabel = [selectIcons objectAtIndex:1];
				NSArray *icons = [iconLabel componentsSeparatedByString:@","];
				for (int j = 0; j < [icons count]; j++) {
					NSString *icon = [icons objectAtIndex:j];
					UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(32 * j, 0, 24, 24)];
					imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
					imageView.backgroundColor = [UIColor clearColor];
					[valueField addSubview:imageView];
					[imageView release];
				}
				return cell;
			}
		} else if (cFieldType == 'N') {
			UIWebView *checkImage = (UIWebView *)[cell.contentView viewWithTag:-3];
			NSString *checkImageUrl = [NSString stringWithFormat:@"%@%@%@%@%@", self.userDefaults.current_ServerUrl, @"/beapp/checkcode?d=",dataObjectName,@"&f=",[MCUIToolkit getFieldName:appItem]];
			checkImage.scalesPageToFit = NO;
			[checkImage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:checkImageUrl]]];
		}
//        NSLog(@"MCControl_TextField=%d",MCControl_TextField);
		if (valueField.style == MCControl_TextField) {
			[(UITextField*)valueField.control addTarget:self
												 action:@selector(textFieldDone:)
									   forControlEvents:UIControlEventEditingDidEndOnExit];
			((UITextField*)valueField.control).delegate = self;
		}
        
		valueField.text = valueText;
        
//        NSLog(@"labelField.text1=%@ --=%@~~~~~~~~~%@",labelField.text,(UITextField *)valueField.control,valueField.text);
        
#pragma mark ~~~~~~~~~~~~~   添加经纬度   ~~~~~~~~~~~~
//        NSString *lblString = [formLayoutCopy objectForKey:@"lbl"];
//        if ([lblString isEqualToString:@"位置上报"]||[lblString isEqualToString:@"巡检点录入"]||[lblString isEqualToString:@"基站录入"]||[lblString isEqualToString:@"巡检点修改"]) {
        
        if ([labelField.text isEqualToString:@"纬度"] ) {
//            [self getTheCoordinage];
//            ((UITextField*)valueField.control).delegate = self;
            
            self.weiField = (UITextField *)valueField.control;
        }
        if (  [labelField.text isEqualToString:@"经度"]) {
//            [self getTheCoordinage];
//            ((UITextField*)valueField.control).delegate = self;
            self.jingField = (UITextField *)valueField.control;
            
        }
            
	
	} else {
		MCSwitch *valueField = (MCSwitch *)[cell.contentView viewWithTag:-2];
		[valueField addTarget:self action:@selector(switchValue:) forControlEvents:UIControlEventValueChanged];
		if ([valueText isEqualToString:@"☑"]) {
			valueField.on = YES;
		}else {
			valueField.on = NO;
		}
	}
    
	return cell;
}

//白菜 利用延时可以解决 禁止修改同时能将数据传输的问题
- (void)lockLocationField
{
    
    [self.jingField resignFirstResponder];
    self.jingField.userInteractionEnabled = NO;
    [self.weiField resignFirstResponder];
    self.weiField.userInteractionEnabled = NO;
}
#pragma mark ---- 事件处理方法 电话号码验证----
- (void) selectAction: (id) sender {
	[[self findFirstResponder] resignFirstResponder];
	if ([DateTimeSelectView instance].isShown) {
		[[DateTimeSelectView instance] selectCancelAction];
	}
	if ([LookupSelectView instance].isShown) {
		[[LookupSelectView instance] selectCancelAction];
	}
	if ([editButtons count] == 1) {
		NSMutableDictionary *button = [editButtons objectAtIndex:0];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		bool pageCheck = NO;
		if ([button objectForKey:@"pck"]) {
			pageCheck = [[button objectForKey:@"pck"]boolValue];
		}
		[self setDataBeforeCommit];
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		if (pageCheck) {
			if ([self isFormVaild]) {
				NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												   buttonId,@"buttonId",
												   @"edit_form",@"buttonType",
												   self.dataObjectName,@"dataObjectName",
												   self.formData,@"formData", nil];
				if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
					[MCFunction pushViewByUrl:[button objectForKey:@"url"]
								   parameters:parameters
							   confirmMessage:[button objectForKey:@"cfmi"]
							   naviController:self.navigationController];
				}else{
                    //联系电话的号码
                    NSString *lxdh = [[[[parameters objectForKey:@"formData"]objectForKey:@"data"]objectForKey:@"lxdh"]objectForKey:@"value"];//电话号码校验 2013-10-28 17:35:26
                    //联系电话
                    
                    for (NSMutableDictionary *section in [formLayoutCopy objectForKey:@"stn"]) {
                        for (NSMutableDictionary *appItem in [section objectForKey:@"item"]) {
                            //                            NSLog(@"appitem=%@",appItem);
                            if (![[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$"] evaluateWithObject:lxdh]&&[[[appItem objectForKey:@"fld"]objectForKey:@"lbl"] isEqualToString:@"联系电话"]) {
                                
                                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的电话号码！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                                
                                [alert show];
                                [alert release];
                                return;//这个用法比较好
                            }
                        }
                    }
                }
                
                [MCFunction pushViewByUrl:[button objectForKey:@"url"]
                               parameters:parameters
                           confirmMessage:nil
                           naviController:self.navigationController];
				[parameters release];
			}
		}else {
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"edit_form",@"buttonType",
											   self.dataObjectName,@"dataObjectName",
											   self.formData,@"formData", nil];
			
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"]
							   parameters:parameters
						   confirmMessage:[button objectForKey:@"cfmi"]
						   naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"]
							   parameters:parameters
						   confirmMessage:nil
						   naviController:self.navigationController];
			}
			[parameters release];
		}
		return;
	}
	
	//导航bar上面的操作按钮弹出菜单
	UIActionSheet *menu = [MCUIToolkit actionSheet:editButtons
										   withTag:2
									  withDelegate:self
										 withTitle:@"选择操作"];
	[menu showFromTabBar:(UITabBar *)[self.navigationController view]];
}

@end

